package com.atguigu.tingshu.search.consumer;

import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.search.service.MqOpsService;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
@Slf4j
public class SearchInfoReceiver {
    @Autowired
    private MqOpsService mqOpsService;

    @SneakyThrows   // 可以绕开编译期间的异常 等运行时发现异常才抛出
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(
                    value = MqConst.EXCHANGE_ALBUM,
                    type = "direct",
                    durable = "true"
            ),
            value = @Queue(
                    value = MqConst.QUEUE_ALBUM_UPPER,
                    durable = "true",
                    exclusive = "false"
            ),
            key = MqConst.ROUTING_ALBUM_UPPER
    ))
    public void listenOnSaleAlbum(String albumId, Message message, Channel channel) {
        log.info("搜索微服务从指定队列中监听到上游专辑微服务的消息：{}完成专辑上架", albumId);
        if (StringUtils.hasText(albumId)) {
            mqOpsService.listenOnSaleAlbum(albumId);
            long deliveryTag = message.getMessageProperties().getDeliveryTag();
            channel.basicAck(deliveryTag, false);
        }
    }

    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(
                    value = MqConst.QUEUE_ALBUM_LOWER,
                    durable = "true",
                    exclusive = "false"
            ),
            exchange = @Exchange(
                    value = MqConst.EXCHANGE_ALBUM,
                    type = "direct",
                    durable = "true"
            ),
            key = MqConst.ROUTING_ALBUM_LOWER
    ))
    public void listenOffSaleAlbum(String albumId, Message message, Channel channel) {
        if (StringUtils.hasText(albumId)) {
            mqOpsService.listenOffSaleAlbum(albumId);
            long deliveryTag = message.getMessageProperties().getDeliveryTag();
            channel.basicAck(deliveryTag, false);
        }
    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = MqConst.QUEUE_ALBUM_ES_STAT_UPDATE, durable = "true", exclusive = "false"),
            exchange = @Exchange(value = MqConst.EXCHANGE_ALBUM),
            key = MqConst.ROUTING_ALBUM_ES_STAT_UPDATE))
    @SneakyThrows   // 可以绕开编译期间的异常 等运行时发现异常才抛出
    public void listenAlbumStatUpdate(String content, Message message, Channel channel) {

        log.info("搜索微服务从指定队列中监听到上游专辑微服务的消息：{}完成专辑下架", content);
        // 1.获取消息，进行判断
        if (!StringUtils.hasText(content)) {
            return;
        }
        // 2.消费消息
        mqOpsService.updateAlbumStat(content);

        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        // 3.手动应答
        channel.basicAck(deliveryTag, false);
    }
}